package com.ljh.web;

import com.ljh.bean.Student;
import com.ljh.bean.Users;
import com.ljh.service.RoleService;
import com.ljh.service.StudentService;
import com.ljh.service.UsersService;
import com.ljh.service.impl.RoleServiceImpl;
import com.ljh.service.impl.StudentServiceImpl;
import com.ljh.service.impl.UsersServiceImpl;
import com.ljh.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = {"/power/user/users"})
public class UsersServlet  extends HttpServlet {
    private UsersService usersService=new UsersServiceImpl();
    private RoleService roleService = new RoleServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if("select".equals(method)){
            select(req,resp);
        }else if("update".equals(method)) {
            update(req, resp);
        }else if("findbyid".equals(method)){
            findbyid(req, resp);
        }else if("deleteuser".equals(method)) {
            deleteuser(req, resp);
        }else if("insert".equals(method)){
            insert(req, resp);
        } else if ("deleteUsers".equals(method)){
            deleteUsers(req, resp);
        }
    }

    protected void deleteUsers(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String loginNames = req.getParameter("loginNames");
        //根据主键查询用户信息
        int i = usersService.delUsers(loginNames);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/power/user/users?method=select'</script>");
        }
    }

    //查询数据（分页）
    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.接收参数(分页参数+模糊查参数)
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);
        //2.调取service方法(1.查询数据列表的方法  2.查询总条数的方法)
        PageUtil pageUtil = new PageUtil();
        List<Users> usersList = usersService.getUsersList(pageIndex, pageUtil.getPageSize());
        int total = usersService.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(usersList);

        //3.存值跳页面
        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);
    }

    //主键查询
    protected void findbyid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String sid = req.getParameter("sid");
        //根据主键查询用户信息
        Users user = usersService.findById(Integer.parseInt(sid));
        List roleList = roleService.getList();
        req.setAttribute("rolelist",roleList);
        req.setAttribute("use",user);
        req.getRequestDispatcher("edit.jsp").forward(req,resp);
    }

    //修改学员
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginName = req.getParameter("loginName");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String roleId = req.getParameter("roleId");
        String userId = req.getParameter("userId");

        //步骤2
        Users user = new Users();
        user.setLoginName(loginName);
        user.setRealName(realName);
        user.setUserId(Integer.parseInt(userId));
        user.setPhone(phone);
        user.setAddress(address);
        user.setRoleId(Integer.parseInt(roleId));
        user.setSex(Integer.parseInt(sex));
        user.setEmail(email);

        UsersService service=new UsersServiceImpl();
        int i = service.updateUser(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('更新成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('更新失败');location.href='/power/user/users?method=findbyid&sid="+userId+"'</script>");
        }
    }

    //删除用户
    protected void deleteuser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sid");
        //根据主键查询用户信息
        int i = usersService.delUser(sid);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('删除成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('删除失败');location.href='/power/user/users?method=select'</script>");
        }
    }

    //新增用户
    protected void insert(HttpServletRequest req,HttpServletResponse resp) throws IOException{
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String address = req.getParameter("address");
        String phone = req.getParameter("phone");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");
        String roleId = req.getParameter("roleId");
        //调取service
        UsersService service=new UsersServiceImpl();
        //将参数封装到学生对象中
        Users user = new Users();
        user.setLoginName(loginName);
        user.setPassWord(password);
        user.setRealName(realName);
        user.setSex(Integer.parseInt(sex));
        user.setEmail(email);
        user.setAddress(address);
        user.setPhone(phone);
        user.setCardId(cardId);
        user.setDesc(desc);
        user.setRoleId(Integer.parseInt(roleId));
        int i = service.insertUser(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if(i>0){
            writer.println("<script>alert('新增成功');location.href='/power/user/users?method=select'</script>");
        }else{
            writer.println("<script>alert('新增失败');location.href='/power/user/users?method=select'</script>");
        }
    }


}
